package com.lemon.controller;

import com.lemon.entity.Book;
import com.lemon.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    private final BookService bookService;

    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @GetMapping
    public List<Book> query() {
        return this.bookService.queryBookForList();
    }

    @GetMapping("/{id}")
    public Book selectById(@PathVariable("id") Integer id) {
        return this.bookService.selectBookById(id);
    }

    @PostMapping// PUT DELETE都类似
    public Book save(@RequestBody Book book) {
        bookService.insertBook(book);
        // 完成 POST => insert
        // PUT => update
        // DELETE => delete
        return book;
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable("id") Integer id) {
        int rows = this.bookService.deleteBook(id);
        return rows;
        //return 200;
    }

      @PutMapping
    public Book update(@RequestBody Book book) {
        this.bookService.updateBook(book);
        return book;
    }

}
